﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 观察者模式.Observer;

namespace 观察者模式.Subject
{
    /// <summary>
    /// 事情起源
    /// 猫叫
    /// </summary>
    public class Cat
    {
       
        public void Miao()
        {
            Console.WriteLine($"{this.GetType().Name}Miao一声");

            //一系列后续动作
            
            new Baby().Cry();
            new Dog().Wang();
            new Father().Roar();
            new Mother().Whisper();

        }

        private List<IObserver> _Observers = new List<IObserver>();

        /// <summary>
        /// 注册Observer
        /// </summary>
        public void AddObserver(IObserver observer)
        {
            this._Observers.Add(observer);
        }


        public void RemoveObserver(IObserver observer)
        {
            this._Observers.Remove(observer);
        }
        public void MiaoObserver()
        {
            Console.WriteLine($"{this.GetType().Name}Miao一声");

            foreach (var observer in _Observers)
            {
                observer.Action();
            }
        }


        public event Action MiaoEvent;

        public void MiaoEvent1()
        {
            Console.WriteLine($"{this.GetType().Name}Miao一声");
         
            if (this.MiaoEvent != null)
            {
                MiaoEvent.Invoke();
            }
        }
        
    }
}
